<?php 
$this->headScript()
		->appendFile($this->baseUrl()."/js/jquery/development-bundle/ui/jquery.ui.core.js")
		->appendFile($this->baseUrl()."/js/jquery/development-bundle/ui/jquery.ui.widget.js")
		->appendFile($this->baseUrl()."/js/jquery/development-bundle/ui/jquery.ui.button.js");
$this->headLink()->appendStylesheet($this->baseUrl(). '/js/jquery/development-bundle/themes/ui-lightness/jquery.ui.all.css');

$addproduct=	false;
$addcategory=	false;
$parent=$this->CatParent;

$categoria="Principal";

if(!empty($this->CatInfo['Name'])){
	$categoria=$this->CatInfo['Name'];
}
$this->headTitle('Categorías');
$this->headTitle('Productos');

?>
<div class="section table_section">
<div class="section_inner">
	<div class="title_wrapper">
		<h2 id="title-section"></h2>
	</div>
	<div class="section_content">
	<p class="errorText">&nbsp;<?php echo $this->messageError; ?></p>
	<div  id="product_list">
	<!--[if !IE]>start table_wrapper<![endif]-->
	<div class="table_wrapper">
		<div class="table_wrapper_inner">
		
                <table style="width: 100%; border: none; padding: 0">
                <tbody>
                	<tr>
		              	<th width="20" align="right">ID</th>
		                <th class="photo"><span>Imagen</span></th>		              	
		                <th>Categorías / Productos</th>
		                <th align="left">Fabricante</th>
		                <th align="right">Precio</th>
		                <th align="right">Cantidad&nbsp;&nbsp;&nbsp;</th>
		                <th width="50" align="center">Estado</th>
		                <th align="right">Acción&nbsp;</th>
              		</tr>
		<?php 

			foreach ($this->Categories AS $dat){
				if($dat['Type']=='Product'){
					$addproduct=true;
					$newprice=0;
					$image_url=$this->baseUrl()."/images/noimage-product_Mini_Thumb.jpg";
					if(is_array($dat['AllImagesByProduct'])){
						$totimg=count($dat['AllImagesByProduct']);
						$image_url=$this->baseUrl()."/images/files/product/Mini_Thumb/".$dat['AllImagesByProduct'][rand(0, ($totimg-1))]['MiniThumb'];
					}
					
					$newprice=$dat['Price'];
					
					if(!empty($dat['TaxPercent']) && $dat['TaxPercent'] > 0){
						$newprice = $dat['Price'] * (($dat['TaxPercent'] / 100) + 1);
					}
				}
				else{
					$image_url=$this->baseUrl()."/images/noimage-category_Thumb.jpg";
					if(!empty($dat['Thumb'])){
						$image_url=$this->baseUrl()."/images/files/category/Thumb/".$dat['Thumb'];
					}
					$addcategory=true;
				}
		?>
		
              <tr class="dataTableRow" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href='<?php echo $this->baseUrl(); ?>/admin/<?php echo (($dat['Type']=='Product')) ? 'products/add' : 'categories/index'; ?>/Id/<?php echo $dat['Id'];?>/catParent/<?php echo $parent; ?>/'">
                <td width="20" align="right"><?php echo $dat['Id'];?></td>
                <td class="photo">
					<a href="#" class="product_thumb">
						<img src="<?php echo $image_url; ?>" alt="" width="44" height="31" />
					</a>
				</td>
                <td valign="middle">
                	<a class="product_name" href="<?php echo $this->baseUrl(); ?>/admin/<?php echo (($dat['Type']=='Product')) ? 'products/add/catParent/'.$parent : 'categories'; ?>/?Id=<?php echo $dat['Id'];?>">
                		<?php echo (isset($dat['Name'])) ? $dat['Name'] : '&nbsp;&nbsp;'; ?>
					</a>
                </td>
                <td align="center" valign="middle"><?php echo (isset($dat['Manufacturer'])) ? $dat['Manufacturer'] : '&nbsp;&nbsp;'; ?></td>
                <td align="right" valign="middle"><?php echo (isset($newprice)) ? (sprintf("%01.2f", $newprice)) : '&nbsp;&nbsp;'; ?></td>
                <td align="right" valign="middle"><?php echo (isset($dat['Cantidad'])) ? $dat['Cantidad'] : '&nbsp;&nbsp;'; ?></td>
                <td width="50" align="center">
                	<?php echo ($dat['Status']=="1")? '<span class="approved">Activo</span>' : '<span class="pending">Inactivo</span>'; ?>                	
				</td>
                <td align="right">
                <div class="actions_menu">
					<ul>
						<li><a class="edit" href="<?php echo $this->baseUrl(); ?>/admin/<?php echo (($dat['Type']=='Product')) ? 'products' : 'categories'; ?>/add/Id/<?php echo $dat['Id'];?>/catParent/<?php echo $parent; ?>/">editar</a></li>
						<li><a class="delete delete-alert" href="<?php echo $this->baseUrl(); ?>/admin/<?php echo (($dat['Type']=='Product')) ? 'products' : 'categories'; ?>/delete/Id/<?php echo $dat['Id'];?>/catParent/<?php echo $parent; ?>/">
							<input type="hidden" name="catprod" id="catprod" value="<?php echo (($dat['Type']=='Product')) ? 'producto' : 'categoria'; ?>" />Eliminar</a>
						</li>
					</ul>
				</div>
					
               </td>
              </tr>
<?php 	} ?>              
        </table>
        </div>
</div>
	<!--[if !IE]>end table_wrapper<![endif]-->
	</div>	
<?php echo $this->paginationControl($this->Categories, 'Sliding', 'pagination.phtml'); ?>
		</div>
	</div>        

<?php 
if(!$addcategory && !$addproduct){
	$addcategory=true;
	$addproduct=true;
} ?>
                <table border="0" width="100%" cellspacing="0" cellpadding="2">
                  <tr>
                    <td class="smallText">Categorías:&nbsp;<?php echo ($addcategory) ? $this->Categories->getTotalItemCount() : 0; ?><br />Productos:&nbsp;<?php echo ($addproduct) ? $this->Categories->getTotalItemCount() : 0; ?></td>
                    <td align="right" class="smallText">
<?php if(isset($this->CatInfo['IdParent'])) { ?>
						<a href="<?php echo $this->baseUrl(); ?>/admin/categories/index/Id/<?php echo $this->CatInfo['IdParent'];?>/" class="buttonjui">Regresar</a>
<?php } if($addproduct) { ?>
						<a href="<?php echo $this->baseUrl(); ?>/admin/products/add/catParent/<?php echo $parent; ?>/" class="buttonjui">Nuevo Producto</a>
<?php } if($addcategory) { ?>
						<a href="<?php echo $this->baseUrl(); ?>/admin/categories/add/catParent/<?php echo $parent;?>/" class="buttonjui">Nueva categoría</a>
			AVISO: Esta categoría tiene subcategorías.<br /> No pueden añadirse productos.&nbsp;
<?php } 
?>
					</td>
                  </tr>
                </table>
<script type="text/javascript">
$(document).ready(function(){
	$( ".buttonjui").button();
	
	var catprod="<?php echo ($addproduct)? "Productos" : "Categorías de productos"; ?>";
	$("#title-section").html(catprod);
	$("a.delete-alert").click(function(){
		var catprod = ($(this).find('input[type=hidden]').val());

		if(catprod=='producto'){
			if(confirm("Realmente desea eliminar este producto?, tenga encuenta que este proceso será irreversible")){ return true; }
		}
		else{
			if(confirm("Relamente desea eliminar esta categoría? esto eliminará todas las subcategorías dentro de esta, así como sus respectivos productos.")){ return true; }
		}
		
		return false;
	});
	
});
</script>
</div>

